<?php

/**
 * @file
 * The default format for Viet Nam adresses.
 */

$plugin = array(
  'title' => t('Address form specific for Viet Nam (FFS)'),
  'format callback' => 'addressfieldvn_format_address_generate',
  'type' => 'address',
  'weight' => -100,
);

/**
 * Format callback for Viet Nam address.
 *
 * @see CALLBACK_addressfield_format_callback()
 */
function addressfieldvn_format_address_generate(&$format, $address, $context = array()) {
  if ($address['country'] == 'VN') {
    unset($format['locality_block']['administrative_area']);

    $format['locality_block']['locality'] = array(
      '#title' => t('City/Province'),
      '#options' => _addressfieldvn_city_list(),
      '#required' => TRUE,
      '#attributes' => array('class' => array('locality')),
      '#weight' => 1,
    );
    $format['locality_block']['dependent_locality'] = array(
      '#title' => t('District'),
      '#size' => 20,
      '#tag' => 'div',
      '#attributes' => array('class' => array('dependent', 'district')),
      '#weight' => 2,
    );
    $format['locality_block']['postal_code'] = array(
      '#title' => t('Postal Code'),
      '#size' => 10,
      '#required' => TRUE,
      '#attributes' => array('class' => array('postal-code')),
      '#weight' => 3,
    );
  }
  // Format render
  if ($context['mode'] == 'render') {
    $format['locality_block']['dependent_locality']['#weight'] = 1;
    $format['locality_block']['locality']['#weight'] = 2;
    $format['locality_block']['postal_code']['#weight'] = 3;
    $format['locality_block']['postal_code']['#prefix'] = ' ';
  }
  // Check ajax form in Viet Nam
  if ($context['mode'] == 'form' && $address['country'] == 'VN') {
    $format['locality_block']['locality']['#ajax'] = array(
      'callback' => 'addressfield_standard_widget_refresh',
      'wrapper' => $format['#wrapper_id'],
      'method' => 'replace',
    );
  } else {
    if (isset($format['locality_block']['locality'])) {
      $format['locality_block']['locality']['#ajax'] = array();
    }
  }
}

/**
 * The list city/province in Viet Nam.
 */
function _addressfieldvn_city_list() {
  // Using http://adslvnpt.com/dien-thoai-co-dinh-va-gphone/huong-dan-su-dung/bang-ma-vung
  $data = array(
    '' => t('--'),
    4 => t('Ha Noi'),
    8 => t('Ho Chi Minh'),
    511 => t('Da Nang'),
    31 => t('Hai Phong'),
    710 => t('Can Tho'),
    76 => t('An Giang'),
    64 => t('Ba Ria - Vung Tau'),
    781 => t('Bac Lieu'),
    240 => t('Bac Giang'),
    281 => t('Bac Can'),
    241 => t('Bac Ninh'),
    75 => t('Ben Tre'),
    650 => t('Binh Duong'),
    651 => t('Binh Phuoc'),
    62 => t('Binh Thuan'),
    780 => t('Ca Mau'),
    26 => t('Cao Bang'),
    500 => t('Dac Lac'),
    501 => t('Dac Nong'),
    230 => t('Dien Bien'),
    61 => t('Dong Nai'),
    67 => t('Dong Thap'),
    59 => t('Gia Lai'),
    219 => t('Ha Giang'),
    351 => t('Ha Nam'),
    39 => t('Ha Tinh'),
    320 => t('Hai Duong'),
    711 => t('Hau Giang'),
    218 => t('Hoa Binh'),
    321 => t('Hung Yen'),
    58 => t('Khanh Hoa'),
    77 => t('Kien Giang'),
    60 => t('Kon Tum'),
    231 => t('Lai Chau'),
    63 => t('Lam Dong'),
    25 => t('Lang Son'),
    20 => t('Lao Cai'),
    72 => t('Long An'),
    350 => t('Nam Dinh'),
    38 => t('Nghe An'),
    30 => t('ninh Binh'),
    68 => t('Ninh Thuan'),
    210 => t('Phu Tho'),
    57 => t('Phu Yen'),
    52 => t('Quang Binh'),
    510 => t('Quang Nam'),
    55 => t('Quang Ngai'),
    33 => t('Quang Ninh'),
    53 => t('Quang Tri'),
    79 => t('Soc Trang'),
    22 => t('Son La'),
    66 => t('Tay Ninh'),
    36 => t('Thai Binh'),
    280 => t('Thai Nguyen'),
    37 => t('Thanh Hoa'),
    54 => t('Thua Thien - Hue'),
    73 => t('Tien Giang'),
    74 => t('Tra Vinh'),
    27 => t('Tuyen Quang'),
    70 => t('Vinh Long'),
    211 => t('Vinh Phuc'),
    29 => t('Yen Bai')
  );

  $data = array(
    '--' => t('--'),
    t('Ha Noi') => t('Ha Noi'),
    t('Ho Chi Minh') => t('Ho Chi Minh'),
    t('Da Nang') => t('Da Nang'),
    t('Hai Phong') => t('Hai Phong'),
    t('Can Tho') => t('Can Tho'),
    t('An Giang') => t('An Giang'),
    t('Ba Ria - Vung Tau') => t('Ba Ria - Vung Tau'),
    t('Bac Lieu') => t('Bac Lieu'),
    t('Bac Giang') => t('Bac Giang'),
    t('Bac Can') => t('Bac Can'),
    t('Bac Ninh') => t('Bac Ninh'),
    t('Ben Tre') => t('Ben Tre'),
    t('Binh Duong') => t('Binh Duong'),
    t('Binh Phuoc') => t('Binh Phuoc'),
    t('Binh Thuan') => t('Binh Thuan'),
    t('Ca Mau') => t('Ca Mau'),
    t('Cao Bang') => t('Cao Bang'),
    t('Dac Lac') => t('Dac Lac'),
    t('Dac Nong') => t('Dac Nong'),
    t('Dien Bien') => t('Dien Bien'),
    t('Dong Nai') => t('Dong Nai'),
    t('Dong Thap') => t('Dong Thap'),
    t('Gia Lai') => t('Gia Lai'),
    t('Ha Giang') => t('Ha Giang'),
    t('Ha Nam') => t('Ha Nam'),
    t('Ha Tinh') => t('Ha Tinh'),
    t('Hai Duong') => t('Hai Duong'),
    t('Hau Giang') => t('Hau Giang'),
    t('Hoa Binh') => t('Hoa Binh'),
    t('Hung Yen') => t('Hung Yen'),
    t('Khanh Hoa') => t('Khanh Hoa'),
    t('Kien Giang') => t('Kien Giang'),
    t('Kon Tum') => t('Kon Tum'),
    t('Lai Chau') => t('Lai Chau'),
    t('Lam Dong') => t('Lam Dong'),
    t('Lang Son') => t('Lang Son'),
    t('Lao Cai') => t('Lao Cai'),
    t('Long An') => t('Long An'),
    t('Nam Dinh') => t('Nam Dinh'),
    t('Nghe An') => t('Nghe An'),
    t('ninh Binh') => t('ninh Binh'),
    t('Ninh Thuan') => t('Ninh Thuan'),
    t('Phu Tho') => t('Phu Tho'),
    t('Phu Yen') => t('Phu Yen'),
    t('Quang Binh') => t('Quang Binh'),
    t('Quang Nam') => t('Quang Nam'),
    t('Quang Ngai') => t('Quang Ngai'),
    t('Quang Ninh') => t('Quang Ninh'),
    t('Quang Tri') => t('Quang Tri'),
    t('Soc Trang') => t('Soc Trang'),
    t('Son La') => t('Son La'),
    t('Tay Ninh') => t('Tay Ninh'),
    t('Thai Binh') => t('Thai Binh'),
    t('Thai Nguyen') => t('Thai Nguyen'),
    t('Thanh Hoa') => t('Thanh Hoa'),
    t('Thua Thien - Hue') => t('Thua Thien - Hue'),
    t('Tien Giang') => t('Tien Giang'),
    t('Tra Vinh') => t('Tra Vinh'),
    t('Tuyen Quang') => t('Tuyen Quang'),
    t('Vinh Long') => t('Vinh Long'),
    t('Vinh Phuc') => t('Vinh Phuc'),
    t('Yen Bai') => t('Yen Bai')
  );

  return $data;
}
